package com.xc58.highreactor.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class IOUtils {
	public static byte[] toByteArray(InputStream input)
			throws IOException{
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		copy(input, output);
		byte[] result = output.toByteArray();
		output.close();
		input.close();
		return result;
	}	

	public static int copy(InputStream input, OutputStream output)
			throws IOException{
		long count = copyLarge(input, output);
		if (count > 2147483647L) {
			return -1;
		}
		return (int)count;
	}

	public static long copyLarge(InputStream input, OutputStream output)
			throws IOException{
		byte[] buffer = new byte['?'];
		long count = 0L;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}
}
